package com.moneytalks.blaze.poc;

import javax.sql.DataSource;

import org.hsqldb.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Wrapps the HSQL Server for startup in the applicationContext
 */
public class DbServerWrapper {

	@Autowired
	private DataSource dataSource;

	public DbServerWrapper(String dbName) {

		String dbPath = System.getProperty("user.home") + "/dbName";

		Server db = new Server();
		db.setDatabaseName(0, dbName);
		db.setDatabasePath(0, dbPath + "/" + dbName);
		db.setLogWriter(null);
		db.setErrWriter(null);
		db.setSilent(true);
		db.start();
	}

	/**
	 * Method shuts down the HSQLDB.
	 */
	public void shutdown() {
		new JdbcTemplate(dataSource).execute("shutdown");
	}
}
